package com.deyuanyun.pic.pipe.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.dto.BasicDataByFind;
import com.deyuanyun.pic.common.dto.DTOResult;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.domain.base.UnifiedEncoding;
import com.deyuanyun.pic.domain.pipe.PipeSegment;
import com.deyuanyun.pic.mapper.base.UnifiedEncodingMapper;
import com.deyuanyun.pic.pipe.domain.exceldomain.PipeTeeImportDemo;
import com.deyuanyun.pic.pipe.mapper.DyyPipeLineMapper;
import com.deyuanyun.pic.pipe.mapper.TeeMapper;
import com.deyuanyun.pic.pipe.service.TeeService;
import com.deyuanyun.pic.pipe.support.utils.UniformCodeUtils;
import com.deyuanyun.pic.service.pipe.PipeSegmentService;
import com.deyuanyun.pic.support.biz.CodeUtil;
import com.github.pagehelper.Page;
/**
 * 
 * @ClassName TeeServiceImpl
 * @Description 管道基础数据 ->三通service实现
 * @author 王敏
 * @date 2015年1月18日
 */
@Service
public class TeeServiceImpl implements TeeService {
	@Resource
	private PipeSegmentService pipeSegmentService;
	@Resource
	TeeMapper teeMapper;
	
	@Resource
	private UnifiedEncodingMapper unifiedEncodingMapper;
	
	@Resource
	private DyyPipeLineMapper dyyPipeLineMapper;
		/**
		* @Description: 根据条件查询
		* @Author: 荣华兴
		* @Create Date: 2015年8月31日
		* @参数：@param bData，对象中包含：管道id、管段id、起始里程、终点里程
		* @Return:List
	 */
	/*@Override
	public DTOResult findByCondition(BasicDataByFind bData) {
		
				String pipeLineId=bData.getLineId();
				List list=teeMapper.findByCondition(bData);
				return new DTOResult(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getLabel(), list);
			
	}*/
		/**
		* @Description: 根据传入Id，查询对应Id的详细数据
		* @Author: 荣华兴
		* @Create Date: 2015年8月31日
		* @参数：@param id
		* @参数：@return
		* @Return:DTOResult
	 */
	@Override
	public DTOResult findById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<HashMap<String, Object>> findByCondition(
			BasicDataByFind bData) {
		// TEETYPE,TEEMATERIAL,BRANCHDIAMETER,OUTLETWALLTHICKNESS,OUTLETCONNECTIONTYPE
		
		Page<HashMap<String, Object>> list2 = new Page<HashMap<String, Object>>();
		Page<HashMap<String, Object>> list = teeMapper.findByCondition(bData);
		for (int i = 0; i < list.size(); i++) {
			HashMap<String, Object> map = list.get(i);
			map.put("TEETYPE", CodeUtil.getName((String) map.get("TEETYPE")));
			map.put("TEEMATERIAL",
					CodeUtil.getName((String) map.get("TEEMATERIAL")));
			map.put("BRANCHDIAMETER",
					CodeUtil.getName((String) map.get("BRANCHDIAMETER")));
			map.put("OUTLETWALLTHICKNESS",
					CodeUtil.getName((String) map.get("OUTLETWALLTHICKNESS")));
			map.put("OUTLETCONNECTIONTYPE",
					CodeUtil.getName((String) map.get("OUTLETCONNECTIONTYPE")));
			
			map.put("OUTLET_DIAMETER",
					CodeUtil.getName((String) map.get("OUTLET_DIAMETER")));
			
			list2.add(map);
		}
		list2.setTotal(list.getTotal());
		list2.setPageNum(list.getPageNum());
		list2.setPageSize(list.getPageSize());
		return list2;
	}
		/**
		* @Description: 根据页面传入的值，查询导出值
		* @Author: 荣华兴
		* @Create Date: 2015年8月31日
		* @参数：@param bData，对象中包含：管道id、管段id、起始里程、终点里程
		* @Return:List<List<String[]>> 三通导出列表
	 */
	@Override
	public List<List<String[]>> excelExport(Map map) {
		List<LinkedHashMap<String, Object>> data = teeMapper.exportExcel(map);
		for(HashMap<String, Object> m:data)
		{
			m.put("TEE_TYPE", CodeUtil.getName((String)m.get("TEE_TYPE")));
			m.put("TEE_MATERIAL", CodeUtil.getName((String)m.get("TEE_MATERIAL")));
			m.put("SCRAPERBARIND", CodeUtil.getName((String)m.get("SCRAPERBARIND")));
			m.put("BRANCH_DIAMETER", CodeUtil.getName((String)m.get("BRANCH_DIAMETER")));
			m.put("OUTLET_DIAMETER", CodeUtil.getName((String)m.get("OUTLET_DIAMETER")));
			m.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String)m.get("OUTLET_CONNECTION_TYPE")));
		}
		MapUtil.emptyConvertLink(data);
		List<List<String[]>> result=new ArrayList<List<String[]>>();
		List<String[]> lsr = JXL.toList(data, "yyyy-MM-dd HH:mm:ss");
		result.add(lsr);
		return result;
	}

		@Override
		public boolean importData(List<PipeTeeImportDemo> data) throws Exception {
		    List<Map<String,Object>> insertData=new ArrayList<Map<String,Object>>();
		    Map<String,Object> bean=null;
		    
	
		    data=UniformCodeUtils.sortedByLine(data,"pipeLineId","","code","三通");
			
	    	for(PipeTeeImportDemo info:data)
		    {
	    		//出口连接方式
	    		bean=MapUtil.beanToMap(info);
	    		bean.put("id", UUID.randomUUID().toString().replaceAll("-", ""));
	    		bean.put("createDate", StringUtils.convertDateToString(new Date(), StringUtils.DATE_TIME_DEFAULT));
	    		bean.put("createUser", "");
	    		teeMapper.insertData(bean);
	    		teeMapper.insertPositData(bean);
		    }
			return true;
		}

		@Override
		public Page<HashMap<String, Object>> findPage(HashMap param) {
			
			Page<HashMap<String, Object>> data=teeMapper.findPage(param);
			for(HashMap<String, Object> m:data)
			{
				m.put("TEE_TYPE", CodeUtil.getName((String)m.get("TEE_TYPE")));
				m.put("TEE_MATERIAL", CodeUtil.getName((String)m.get("TEE_MATERIAL")));
				m.put("SCRAPERBARIND", CodeUtil.getName((String)m.get("SCRAPERBARIND")));
				m.put("BRANCH_DIAMETER", CodeUtil.getName((String)m.get("BRANCH_DIAMETER")));
				m.put("OUTLET_DIAMETER", CodeUtil.getName((String)m.get("OUTLET_DIAMETER")));
//				m.put("OUTLET_WALL_THICKNESS", CodeUtil.getName((String)m.get("OUTLET_WALL_THICKNESS")));
				m.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String)m.get("OUTLET_CONNECTION_TYPE")));
			}
			MapUtil.emptyConvert(data);
			return data;
		}

		
		public int delGis(Map map){
			return teeMapper.delGis(map);
		}
		public int delData(Map map){
			teeMapper.delGis(map);
			teeMapper.delData(map);
			return 1;
		}
}
